package project.active;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.pojo.Employee;

import project.adapter.Projectdetail_listerror_adapter_tc;
import project.dao.pr_projectDAO;
import project.model.PrProject;
import project.model.PrTestProject;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import approve.action.R;

public class Projectdetail_listerror_tc extends Activity {

	PrProject pro=new PrProject();
	pr_projectDAO pr=new pr_projectDAO();
	Employee emp=new Employee();
	PrTestProject err=new PrTestProject();
	ListView listerror;
//	Spinner cbfixerror;
	TextView lbstatus,lblisterror;
//	private int status = 0;
	String lang = "EN",database="admin";
	Bundle language;
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.projectdetail_listerror_tc);
		
		language=getIntent().getExtras().getBundle(getLang());
		
		Intent i=this.getIntent();
		pro=(PrProject)i.getSerializableExtra("project");
		emp=(Employee)i.getSerializableExtra("em");
		
//		cbfixerror=(Spinner)findViewById(R.id.cbfixerror);
//		int arr=0;
//		if(getLang().equalsIgnoreCase("EN"))
//			arr=R.array.fix_array_en;
//		else if(getLang().equalsIgnoreCase("VN"))
//			arr=R.array.fix_array_vn;
//		else if(getLang().equalsIgnoreCase("KR"))
//			arr=R.array.fix_array_kr;
//		
//		ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, arr, android.R.layout.simple_spinner_item);
//		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//		cbfixerror.setAdapter(adapter);
//		cbfixerror.setOnItemSelectedListener(listenerSelectedCBFixError);
		
		listerror=(ListView)findViewById(R.id.listerror);
		Projectdetail_listerror_adapter_tc adapter1 = new Projectdetail_listerror_adapter_tc(this, loadAllListError());
		listerror.setAdapter(adapter1);
		
		listerror.setOnItemClickListener(listenerListError_tc);
		
		lbstatus=(TextView)findViewById(R.id.lbstatus);
		lblisterror=(TextView)findViewById(R.id.lblisterror);
		
//		lbstatus.setText(language.getString("ac_Status"));
		lblisterror.setText(language.getString("l_errinfo"));
	}
	
//	private void reloadThisPage(){
//		Projectdetail_listerror_adapter_tc adapter1 = new Projectdetail_listerror_adapter_tc(this, loadAllListError());
//		listerror.setAdapter(adapter1);
//	}
	
//	public OnItemSelectedListener listenerSelectedCBFixError=new OnItemSelectedListener() {
//		public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
//			Toast.makeText(parent.getContext(), "The planet is " +
//			          parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
//			if(parent.getItemAtPosition(position).toString().trim().equalsIgnoreCase("No fix")||
//					parent.getItemAtPosition(position).toString().trim().equalsIgnoreCase("Chưa sửa xong")||
//					parent.getItemAtPosition(position).toString().trim().equalsIgnoreCase("수정 중")){
//				status=0;
//				reloadThisPage();
//			}
//			else{
//				status=1;
//				reloadThisPage();
//			}
//		}
//
//		public void onNothingSelected(AdapterView<?> parent) {
//			// TODO Auto-generated method stub
//			status=0;
//		}
//	};
	
	
	private OnItemClickListener listenerListError_tc=new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view,int position, long id){
			
			AlertDialog.Builder adb=new AlertDialog.Builder(Projectdetail_listerror_tc.this);
			err=(PrTestProject)listerror.getItemAtPosition(position);
			adb.setTitle(err.getTitleview());
			
			adb.setPositiveButton(language.getString("View"), new android.content.DialogInterface.OnClickListener(){
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
//						deleteItemSelected(err.getIdComment());
						Intent i=new Intent(Projectdetail_listerror_tc.this,Projectdetail_error_tc.class);
						if(i!=null)
							loadIntent(i);
					}});
			if(emp.getIdmanager().trim().equalsIgnoreCase(err.getIdmanager().trim())){
				adb.setNeutralButton(language.getString("Delete"), new android.content.DialogInterface.OnClickListener(){
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						try {
							deleteItemSelected();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
//							e.printStackTrace();
						}
					}});
			}
			adb.setNegativeButton(language.getString("ac_Cancel"), null);
			adb.show();
		}
	};
	
	private void loadIntent(Intent i){
		i.putExtras(getIntent().getExtras());
		i.putExtra("errorproject", err);
		i.putExtra("project", pro);
		i.putExtra("lang", lang);
		i.putExtra("database", getDatabase());
		i.putExtra("em", emp);
		startActivity(i);
	}
	
	private void deleteItemSelected() throws SQLException{
		pr.deleteItemSelected_Error(getDatabase(),err.getIdTest());
		Projectdetail_listerror_adapter_tc adapter1 = new Projectdetail_listerror_adapter_tc(this, loadAllListError());
		listerror.setAdapter(adapter1);
	}
	
	public List<PrTestProject> loadAllListError(){
		List<PrTestProject> item=new ArrayList<PrTestProject>();
		item=pr.getLoadAllListErrorDAO(getDatabase(),pro.getIdProject());
		return item;
	}
	
//	private String loadManagerName(String idManager){
//		String name=null;
//		name=pr.getLoadManagerNameByID_tc(getDatabase(),idManager);
//		return name;
//	}
}
